package com.bbg.amyam.web.util.view;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.roo.classpath.operations.AbstractOperations;

import com.bbg.amyam.web.util.menu.MenuItem;
import com.bbg.amyam.web.util.menu.MenuUtil;

public class ViewGenerator extends AbstractOperations {
	private static final Log log = LogFactory.getLog(ViewGenerator.class);

	private List<MenuItem> menuitems;

	private boolean overwrite = false;

	public boolean isOverwrite() {
		return overwrite;
	}

	public void setOverwrite(boolean overwrite) {
		this.overwrite = overwrite;
	}

	public static void main(String[] args) throws FileNotFoundException,
			IOException {
		File file = new File("");

		String path = file.getAbsolutePath();
		log.debug("Current working path is:" + path);
		List<MenuItem> menuitems = MenuUtil.getSchoolMenus();

		ViewGenerator generator = new ViewGenerator(menuitems);
		generator.setOverwrite(false);
		generator.mapulate();

	}

	public ViewGenerator(List<MenuItem> menuitems)
			throws FileNotFoundException, IOException {
		this.menuitems = menuitems;

	}

	public void mapulate() throws IOException {

		for (MenuItem item : this.menuitems) {
			manpulateMenuItem(item);
		}
		(new AmyamLabelGenerate(null, this.overwrite)).saveProperties();
		(new DomainClassNameUpdator(null,this.overwrite)).execute();

	}

	private void manpulateMenuItem(MenuItem item) throws IOException {
		if("webflow".equalsIgnoreCase(item.getType())){
			new WebFlowServicerGenerator (item,this.overwrite).execute();
			new WebFlowViewFilesGenerator (item, this.overwrite).execute();
		}else{
			new ControllerGenerator (item,this.overwrite).execute();
			new ViewFilesGenerator (item, this.overwrite).execute();
		}
	    new ViewFilesGenerator (item, this.overwrite).execute();
		new AmyamLabelGenerate(item, this.overwrite).execute();
		for (MenuItem child : item.getChildren()) {
			manpulateMenuItem(child);
		}
	}

}
